#pragma once
#include "Headerfile.hpp"
// 1. 获取系统时间
//  2. 获取⽂件⼤⼩
//  3. 创建⽬录
//  4. 获取⽂件所在⽬录
namespace shw
{

    namespace icloud
    {
        class FileUtil
        {
        private:
            std::string _filename;

        public:
            FileUtil(const std::string &filename) : _filename(filename)
            {
            }
            size_t GetFileSize() // 获取文件大小
            {
                struct stat _st;

                if (stat(_filename.c_str(), &_st) < 0)
                {
                    std::cout << "get file size failed!\n";
                    return -1;
                }
                return _st.st_size;
            }
            bool GetPosLen(std::string &body, size_t pos, size_t len)
            {
                if (!Exists())
                {
                    cout << "GetPosLen:文件不存在，获取内容失败!\n";
                    return false;
                }
                auto fsize = GetFileSize();
                if (pos + len > fsize)
                {
                    cout << "GetPosLen:读取文件内容太大，请调整大小\n";
                    return false;
                }
                std::ifstream ifs;
                ifs.open(_filename, std::ios::binary); // 打开文件
                if (!ifs.is_open())
                {
                    cout << "GetPosLen:读取打开文件失败\n";
                    return false;
                }
                ifs.seekg(pos, std::ios::beg); // 从文件开始偏移pos
                body.resize(len);
                ifs.read(&(body[0]), len);
                if (!ifs.good())
                {
                    cout << "GetPosLen:读取文件失败！\n";
                    ifs.close();
                    return false;
                }
                ifs.close(); // 走到这就说明成功，关闭文件
                return true;
            }
            bool WriteContet(const std::string &body)
            {
                std::ofstream ofs;
                ofs.open(_filename, std::ios::binary);
                if (!ofs.is_open())
                {
                    cout << "WriteContet:写入打开文件失败！\n";
                    return false;
                }
                ofs.write(&body[0], body.size()); // 写入文件
                if (!ofs.good())
                {
                    cout << "WriteContet:写入文件失败！\n";
                    ofs.close();
                    return false;
                }
                ofs.close();
                return true;
            }
            std::string GetFileName()
            {
                return fs::path(_filename).filename().string();
            }
            bool ScanDir(std::vector<std::string> &arr)
            {
                if (!Exists())
                {
                    cout << "ScanDir:文件不存在，扫描失败！\n";
                    return false;
                }
                for (auto &it : fs::directory_iterator(_filename))
                {
                    if (fs::is_directory(it))
                    {
                        continue;
                    }
                    arr.push_back(fs::path(it).relative_path().string()); // 带有路径的文件名
                }
                return true;
            }
            bool Remove()
            {
                if (this->Exists() == false)
                {
                    return true;
                }
                remove(_filename.c_str());
                return true;
            }
            time_t LastMTime()
            {
                struct stat st;
                if (stat(_filename.c_str(), &st) < 0)
                {
                    std::cout << "get file size failed!\n";
                    return -1;
                }
                return st.st_mtime;
            }
            time_t LastATime()
            {
                struct stat _st;
                if (stat(_filename.c_str(), &_st) < 0)
                {
                    std::cout << "get file size failed!\n";
                    return -1;
                }
                return _st.st_atime;
            }
            bool GetContent(std::string &body)
            {
                size_t fsize = GetFileSize();
                return GetPosLen(body, 0, fsize);
            }
            bool Exists()
            {
                return fs::exists(_filename);
            }
            bool CreDir() // 创建目录
            {
                if (this->Exists())
                    return true;
                return fs::create_directories(_filename);
            }
        };
    }
    namespace util
    {
        class date
        {
        public:
            static time_t Now() // 获取当前时间
            {
                return time(nullptr);
            }
        };

        class file
        {
        public:
            static bool Exist(const std::string &name)
            {
                return fs::exists(name);
            }
            static std::string Path(const std::string &name) // 获取文件路径
            {
                if (name.empty())
                    return ".";
                size_t pos = name.find_last_of("/\\");
                if (pos == std::string::npos)
                    return ".";                 // 没找到
                return name.substr(0, pos + 1); //+1是把'.'也加上
            }
            // ./abc/vs/vsfv/fef/

            static void CreateDirectory(const std::string &path)
            {
                if (Exist(path))
                    return;
                fs::create_directories(path);
            }
        };
    }
}